package com.lc.recovery.ui.activity.recoveryhome;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.load.MultiTransformation;
import com.bumptech.glide.load.resource.bitmap.CenterCrop;
import com.bumptech.glide.load.resource.bitmap.CircleCrop;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.hjq.http.EasyHttp;
import com.hjq.http.listener.HttpCallback;
import com.lc.recovery.R;
import com.lc.recovery.app.AppActivity;
import com.lc.recovery.http.glide.GlideApp;
import com.lc.recovery.http.request.CustormersApi;
import com.lc.recovery.http.response.CustormersBean;
import com.lc.recovery.other.IntentKey;
import com.lc.recovery.util.SPUtil;

import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.List;

import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;
import butterknife.ButterKnife;

public class CustomerActivity extends AppActivity {
    @BindView(R.id.check_danshu)
    CheckBox checkDanshu;
    @BindView(R.id.check_date)
    CheckBox checkDate;
    @BindView(R.id.check_recovery)
    CheckBox checkRecovery;
    @BindView(R.id.rv_recoverymessage)
    RecyclerView rvRecoverymessage;
    @BindView(R.id.tv_orders)
    TextView tvOrders;
    private BaseQuickAdapter<CustormersBean.DataBean, BaseViewHolder> rvRecoverymessageadapter;
    private List<CustormersBean.DataBean> dataBeanList = new ArrayList<>();
    private int page = 1;
    private int order_type = 1;
    private int order_val = 1;

    @Override
    protected int getLayoutId() {

        return R.layout.activity_customer;
    }

    @Override
    protected void initView() {

    }

    @Override
    protected void initData() {

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);

        checkDanshu.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                if (b) {

                } else {

                }

            }
        });
        checkDate.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                if (b) {

                } else {

                }

            }
        });
        checkRecovery.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                if (b) {

                } else {

                }

            }
        });


        rvRecoverymessageadapter = new BaseQuickAdapter<CustormersBean.DataBean, BaseViewHolder>(R.layout.item_customer, dataBeanList) {
            @Override
            protected void convert(@NotNull BaseViewHolder baseViewHolder, CustormersBean.DataBean bean) {
                baseViewHolder.setText(R.id.tv_name, bean.getName());
                baseViewHolder.setText(R.id.tv_recover_count, "共回收" + bean.getOrders() + "单");
                baseViewHolder.setText(R.id.tv_date, "加入日期：" + bean.getCreate_time());
                baseViewHolder.setText(R.id.tv_address, "备注地址：" + bean.getAddress());
                baseViewHolder.setText(R.id.tv_type, bean.getIs_order());
                GlideApp.with(getActivity())
                        .load(bean.getAvatar())
                        .placeholder(R.mipmap.avatar_placeholder_ic)
                        .error(R.mipmap.avatar_placeholder_ic)
                        .transform(new MultiTransformation<>(new CenterCrop(), new CircleCrop()))
                        .into((ImageView) baseViewHolder.getView(R.id.img_head));
                baseViewHolder.getView(R.id.tv_revise).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        startActivity(new Intent(CustomerActivity.this, ReviseActivity.class).putExtra("address", bean.getAddress()).putExtra("id", bean.getId() + ""));
                    }
                });
            }
        };
        View emptyview = View.inflate(getContext(), R.layout.empty_emptylayout, null);
        rvRecoverymessageadapter.setEmptyView(emptyview);
        rvRecoverymessage.setAdapter(rvRecoverymessageadapter);
    }

    @Override
    protected void onStart() {
        super.onStart();
        custormers();
    }

    private void custormers() {
        EasyHttp.post(this)
                .api(new CustormersApi().setUser_id(SPUtil.get(this, IntentKey.USERID, "")).setPage(page + "").setOrder_type(order_type + "").setOrder_val(order_val + "")).request(new HttpCallback<CustormersBean>(this) {

            @Override
            public void onSucceed(CustormersBean data) {
                if (data.getCode() == 200) {
                    dataBeanList.addAll(data.getData());
                    rvRecoverymessageadapter.setNewData(dataBeanList);
                    rvRecoverymessageadapter.notifyDataSetChanged();
                    tvOrders.setText(data.getOrders()+"");
                } else {
                    toast(data.getMsg());
                }


            }


            @Override
            public void onFail(Exception e) {
//                super.onFail(e);

            }
        });
    }


}
